package com.lucenten.collect.config.service.impl;

import javax.annotation.Resource;

import cn.quevo.common.entity.PageResult;

import cn.quevo.common.lang.BooleanUtils;
import cn.quevo.common.lang.StringUtils;
import com.lucenten.collect.config.entity.dto.ThreadStation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lucenten.collect.config.dao.CollectConfDao;
import com.lucenten.collect.config.entity.CollectConf;
import com.lucenten.collect.config.entity.query.CollectConfQuery;
import com.lucenten.collect.config.service.CollectConfService;

import java.util.List;
import java.util.Map;

/**
 * 采集设置 Service接口实现
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2019/04/27 11:04:02
 * @Copyright 东方浩星（北京）传媒科技有限公司版权所有.All Rights Reserved.
 */
@Service("collectConfService")
@Transactional(transactionManager="transactionManager",rollbackFor={Exception.class})
public class CollectConfServiceImpl implements CollectConfService {
    @Resource(name="collectConfDao")
    private CollectConfDao collectConfDao;

    @Override
    public Map<String,Object> getSinge(CollectConfQuery collectConfQuery) {
        return this.collectConfDao.getSinge(collectConfQuery);
    }

    @Override
    public Integer create(CollectConf collectConf) {
        collectConf.validateCreate();
        return this.collectConfDao.create(collectConf);
    }

    @Override
    public Integer update(CollectConf collectConf) {
        collectConf.validateUpdate();
        return this.collectConfDao.update(collectConf);
    }

    @Override
    public PageResult<Map<String,Object>> getPage(CollectConfQuery collectConfQuery) {
        return this.collectConfDao.getPage(collectConfQuery);
    }

    @Override
    public ThreadStation getCollectConfByCode(CollectConfQuery collectConfQuery) {
        return this.initThreadStation(this.collectConfDao.getCollectConfByCode(collectConfQuery));
    }

    private ThreadStation initThreadStation(ThreadStation threadStation){
        CollectConfQuery query = CollectConfQuery.builder().enabledYn(BooleanUtils.TRUE).build();
        query.setIsPaging(false);
        query.setSortName("sortNum");
        query.setSortOrder("ASC");
        List<ThreadStation> list = this.collectConfDao.getCollectConfList(query);
        if (StringUtils.isEmpty(list) || list.size() == 1) {
            threadStation.setBegin(0);
            threadStation.setMaxSize(threadStation.getTotalQty());
            return threadStation;
        }
        int beforeCount = 0;
        // 因为设置的站点总量并不一定等于实际的站点总量，所以先计算设置的总量和实际的总量占比
        double rate = threadStation.getStationCount() * 1d / threadStation.getTotalQty();
        for (ThreadStation station : list) {
            if (station.getId().equals(threadStation.getId())) {
                break;
            }
            // 当前设置站点数量 * 占比得出实际应该处理的站点数量
            beforeCount += station.getStationQty() * rate;
        }
        threadStation.setBegin(beforeCount);
        if (BooleanUtils.isTrue(threadStation.getLastYn())) {
            threadStation.setMaxSize(threadStation.getStationCount() - beforeCount);
        }else {
            threadStation.setMaxSize((int) (threadStation.getStationQty() * rate));
        }
        return threadStation;
    }
}
